package com.zz.ecommerce.goodsManage.goodsBackupInsp;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.bean.Result;
import com.zz.ecommerce.common.bean.insp.db.goodsbackup.TGoodsBackupAttachInsp;
import com.zz.ecommerce.common.bean.insp.db.goodsbackup.TGoodsBackupHeadInsp;
import com.zz.ecommerce.goodsManage.goodsBackupInsp.dao.GoodsBackupAttachInspDao;
import com.zz.ecommerce.goodsManage.goodsBackupInsp.dao.GoodsBackupHeadInspDao;
import com.zz.ecommerce.goodsManage.goodsBackupInsp.service.GoodsBackupInspService;
import com.zz.ecommerce.utils.SessionUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2015/9/10.
 */
@Controller
@RequestMapping("/goodsManage/goodsBackupInsp")
public class GoodsBackupInspController {

    private static final Logger LOG = LoggerFactory.getLogger(GoodsBackupInspController.class);

    @Autowired
    private GoodsBackupHeadInspDao headInspDao;

    @Autowired
    private GoodsBackupAttachInspDao attachInspDao;

    @Autowired
    private GoodsBackupInspService goodsBackupInspService;


    @RequestMapping(value = "init", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/goodsManage/goodsBackupInsp/index";
    }

    @RequestMapping(value = "page", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public GridResult page(TGoodsBackupHeadInsp head, GridRequest gridRequest, HttpSession session) {

        head.setOperId(SessionUtil.getOperator(session));
        head.setOrgId(SessionUtil.getOrganizationId(session));

        List<TGoodsBackupHeadInsp> list = headInspDao.page("T_GOODS_BACKUP_HEAD_INSP", head, gridRequest.getStart(),
                gridRequest.getEnd(), gridRequest.getSortOrder());

        int count = headInspDao.count("T_GOODS_BACKUP_HEAD_INSP",head);

        return new GridResult(list, gridRequest, count);
    }


    @RequestMapping(value = "initEdit", method = {RequestMethod.GET, RequestMethod.POST})
    public String initEdit(TGoodsBackupHeadInsp headInsp, ModelMap modelMap) {
        if (StringUtils.isNotEmpty(headInsp.getId())) {
            headInsp = headInspDao.query("T_GOODS_BACKUP_HEAD_INSP", headInsp);
        }
        else
        {
            headInsp.setGoodsserialno(goodsBackupInspService.getEportSeq());
        }

        modelMap.put("headInsp", headInsp);
        modelMap.put("editable", true);
        return "/goodsManage/goodsBackupInsp/edit";
    }

    @RequestMapping(value = "show", method = {RequestMethod.GET, RequestMethod.POST})
    public String show(TGoodsBackupHeadInsp headInsp, ModelMap modelMap) {
        headInsp = headInspDao.query("T_GOODS_BACKUP_HEAD_INSP", headInsp);
        modelMap.put("headInsp", headInsp);

        modelMap.put("editable", false);
        return "/goodsManage/goodsBackupInsp/edit";
    }

    @RequestMapping(value = "/pageAttach", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public GridResult pageAttach(TGoodsBackupAttachInsp attach, GridRequest request) {
        if (StringUtils.isEmpty(attach.getRelateId())) {
            return new GridResult<TGoodsBackupAttachInsp>(null, request, 0);
        }

        int count = attachInspDao.count("T_GOODS_BACKUP_ATTACH_INSP", attach);
        List<TGoodsBackupAttachInsp> list = attachInspDao.page("T_GOODS_BACKUP_ATTACH_INSP",
                attach, request.getStart(), request.getEnd(), request.getSortOrder());

        return new GridResult<TGoodsBackupAttachInsp>(list, request, count);
    }

    @RequestMapping(value = "/saveHead", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public TGoodsBackupHeadInsp saveHead(TGoodsBackupHeadInsp goodsBasic, HttpSession session) {
        goodsBackupInspService.saveHead(goodsBasic, session);
        return goodsBasic;
    }

    @RequestMapping(value = "/saveAttach", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result saveAttach(TGoodsBackupAttachInsp attach, HttpSession session) {
        Result result = new Result();
        try {
            goodsBackupInspService.saveAttach(attach, session);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);

            result.setCode("0");
            result.setMsg(e.getMessage());
        }

        return result;
    }

    @RequestMapping(value = "/deleteHeadInsp", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result deleteHeadInsp(TGoodsBackupHeadInsp headInsp) {
        Result result = new Result();
        try {
            goodsBackupInspService.deleteHeadInsp(headInsp);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);

            result.setCode("0");
            result.setMsg(e.getMessage());
        }

        return result;
    }

    @RequestMapping(value = "/deleteAttach", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result deleteAttach(TGoodsBackupAttachInsp attach) {
        Result result = new Result();
        try {
            goodsBackupInspService.deleteGoodsAttach(attach);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);

            result.setCode("0");
            result.setMsg(e.getMessage());
        }

        return result;
    }

    @RequestMapping(value = "/declare", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result declare(@RequestParam("id") List<String> idList) {
        Result result = new Result();
        try {
            goodsBackupInspService.declare(idList);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);

            result.setCode("0");
            result.setMsg(e.getMessage());
        }

        return result;
    }

}
